package cn.wyl.order.controller;

import cn.wyl.order.service.IOrderPetAcquisitionService;
import cn.wyl.order.domain.OrderPetAcquisition;
import cn.wyl.order.query.OrderPetAcquisitionQuery;
import cn.wyl.basic.util.AxiousResult;
import cn.wyl.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/orderPetAcquisition")
public class OrderPetAcquisitionController {

    @Autowired
    public IOrderPetAcquisitionService orderPetAcquisitionService;

    /**
     * 接口：添加或修改
     * @param orderPetAcquisition  传递的实体
     * @return AxiousResult 响应给前端
     */
    @PutMapping
    public AxiousResult addOrUpdate(@RequestBody OrderPetAcquisition orderPetAcquisition){
        try {
            if( orderPetAcquisition.getId()!=null)
                orderPetAcquisitionService.update(orderPetAcquisition);
            else
                orderPetAcquisitionService.save(orderPetAcquisition);
            return new AxiousResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AxiousResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AxiousResult 响应给前端
    */
    /*@DeleteMapping(value="/{id}")
    public AxiousResult delete(@PathVariable("id") Long id){
        try {
            orderPetAcquisitionService.remove(id);
            return new AxiousResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AxiousResult(false,"删除失败");
        }
    }*/

    /**
    * 接口：批量删除
    * @param ids
    * @return AxiousResult 响应给前端
    */
    @PatchMapping
    public AxiousResult patchDelete(@RequestBody List<Long> ids){
        try {
                orderPetAcquisitionService.patchDel(ids);
            return new AxiousResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AxiousResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public OrderPetAcquisition findOne(@PathVariable("id")Long id) {
        return orderPetAcquisitionService.loadById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<OrderPetAcquisition> findAll(){
        return orderPetAcquisitionService.loadAll();
    }


   /**
    * 根据登录人employee查看收购订单，平台管理员查看所有收购订单，店铺管理员查看自己店铺收购订单，店员查看自己订单
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<OrderPetAcquisition> queryPage(@RequestBody OrderPetAcquisitionQuery query, HttpServletRequest request) {
        return orderPetAcquisitionService.queryPage(query,request);
    }

    /**
     * 确认收购订单
     * 修改收购订单 money paytype state完成
     * 修改宠物pet costprice
     * 修改寻主消息状态 已完成
     * 前端传入id money paytype
     */
    @PostMapping("/confirm")
    public AxiousResult confirmOrder(@RequestBody Map<String, Object> paramsMap){
        try {
            orderPetAcquisitionService.confirm(paramsMap);
            return new AxiousResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AxiousResult(false,"确认订单失败",null);
        }


    }
    /**
     * 取消订单
     * 修改收购订单 state-1 pet_id null
     * 将寻主消息中的shop_id置空
     * 删除宠物 - 没有成功收购
     * $http.delete
     * /orderPetAcquisition/"+row.id;
     */
    @DeleteMapping("/{id}")
    public AxiousResult cancelOrder(@PathVariable("id") Long id){
        try {
            orderPetAcquisitionService.cancelOrder(id);
            return new AxiousResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AxiousResult(false,"取消订单失败",null);
        }
    }



}
